package com.lyl.label.common.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author Mr.Luo
 * @date 2022-06-02 16:14
 * @desc 通用返回
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> {
    private int code;
    private String message;
    private T data;

    public static <T> Result<T> build(ResultCode res){
        return build(res, null);
    }

    public static <T> Result<T> build(ResultCode res, T data){
        Result<T> result = new Result<>();
        result.setCode(res.getCode())
                .setMessage(res.getMsg())
                .setData(data);
        return result;
    }

    public static <T> Result<T> ok(){
        return build(ResultCode.OK);
    }

    public static <T> Result<T> ok(T data){
        return build(ResultCode.OK,data);
    }

    public static<T> Result<T> fail(){
        return build(ResultCode.FAILED);
    }

    public static<T> Result<T> fail(ResultCode resultCode){
        return build(resultCode);
    }

}
